/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.AdmMateria;
import modelo.Materia;

/**
 *
 * @author DAHER
 */
public class MateriaServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {int tipo=Integer.parseInt(request.getParameter("tipo"));
             
             AdmMateria adm=new AdmMateria();
            switch(tipo){
                case 1:
                    if(adm.agregarMateria(getMateria(request))){
                       response.sendRedirect("exito.jsp");
                            }else{response.sendRedirect("fracaso.jsp");
                    }                        
                    break;
                case 2:if(adm.eliminarMateria(request.getParameterValues("materias"))){
                        response.sendRedirect("index.jsp");
                   }else{response.sendRedirect("fracaso.jsp");
                }
                    break;
                case 3:
                       Materia materia=adm.getMateria(Integer.parseInt(request.getParameterValues("materia")[0]));
                       if(materia!=null){
                         request.setAttribute("mate",materia);
                         request.getRequestDispatcher("actualizar.jsp").forward(request, response);
                       }
                    break;
                case 4:
                    materia=getMateria(request);
                    materia.setId(Integer.parseInt(request.getParameter("id_materia")));
                    if(adm.actualizarMateria(materia)){
                       response.sendRedirect("index.jsp");
                    }else{response.sendRedirect("fracaso.jsp");
                    }
            }
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(MateriaServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Materia getMateria(HttpServletRequest request) {
                
            String nombre_materia=request.getParameter("nombre_mat");        
            String descripcion_materia=request.getParameter("descripcion");
            Materia mate=new Materia(0,nombre_materia,descripcion_materia);
            
        return mate;
    }

    
}
